package com.sunda.spmswms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.Workshop;
import com.sunda.spmswms.mapper.WorkshopMapper;
import com.sunda.spmswms.service.IWorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车间表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2020-12-15
 */
@Service
public class WorkshopServiceImpl extends ServiceImpl<WorkshopMapper, Workshop> implements IWorkshopService {

    @Autowired
    WorkshopMapper workshopMapper;

    @Override
    public List<Map<String, Object>> getWorkshopList() {
        return workshopMapper.getAllWorkshop();
    }

    @Override
    public ResponseResult createWorkshop(String werks, String workshopCode, String workshopNameZh,
                                         String workshopNameEn, String workshopDesc, String userId) {
        try {
            if (StringUtils.isEmpty(werks) || StringUtils.isEmpty(workshopCode) ||
                    StringUtils.isEmpty(workshopNameZh) || StringUtils.isEmpty(workshopNameEn)){
                return ResponseResult.error("请求参数错误请重试");
            }
            QueryWrapper<Workshop> queryWrapper = new QueryWrapper<Workshop>().eq("WERKS", werks)
                    .eq("WORKSHOP_CODE", workshopCode);
            Workshop workshop = this.baseMapper.selectOne(queryWrapper);
            if (workshop != null){
                workshop.setWorkshopNameZh(workshopNameZh);
                workshop.setWorkshopNameEn(workshopNameEn);
                workshop.setWorkshopDesc(workshopDesc);
                this.baseMapper.update(workshop, queryWrapper);
                return ResponseResult.success().add("workshopList", this.baseMapper.selectList(null));
            }
            Workshop workshop1 = new Workshop();
            workshop1.setWerks(werks);
            workshop1.setWorkshopCode(workshopCode);
            workshop1.setWorkshopNameZh(workshopNameZh);
            workshop1.setWorkshopNameEn(workshopNameEn);
            workshop1.setWorkshopDesc(workshopDesc);
            this.baseMapper.insert(workshop1);
            return ResponseResult.success().add("workshopList", this.baseMapper.selectList(null));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求错误请重试");
        }
    }

    @Override
    public Workshop getWorkshopInfo(String werksCode, String workshopCode) {
        QueryWrapper<Workshop> queryWrapper = new QueryWrapper<Workshop>().eq("WERKS", werksCode)
                .eq("WORKSHOP_CODE", workshopCode);
        Workshop workshop = this.baseMapper.selectOne(queryWrapper);
        return workshop;
    }
}
